package com.chengh.nami.core.security.handler;

import com.chengh.nami.common.response.R;
import com.chengh.nami.common.response.ResponseCodeEnum;
import com.chengh.nami.common.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 凭证异常处理
 * 缺少凭证,验证不通过
 *
 * @author chengh
 */
@Slf4j
@Component
public class UserAuthenticationEntryPointHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        log.error("token验证异常...{}", request.getServletPath());
        R<Void> failed = R.failed(ResponseCodeEnum.FAILED_401.getCode(), ResponseCodeEnum.FAILED_401.getMessage());
        ResultUtils.responseJson(response, failed);
    }
}
